/**
 *  获取命令行中的参数
 * @author yangxiao
 * @module lib/argv
 */


export let _isInit = false;
export let _keys:string[]=[];


export let  _keyType = {
    "isdebug":"--isdebug",
    "serverid":"--serverid"
} 

export function getServerid():string{
    return  getCommandArgv(_keyType.serverid);
}

export function getIsDebug():boolean{
    let key =  getCommandArgv(_keyType.isdebug);
    let value = false;
    if(key ==  'true'){
        value =  true;
    } else if(key=='1'){
        value =  true;
    }

    return value;
}



/**
 * 获得命令行中的参数
 * @param key 关键字，可以从argv.KeyType获取
 */
export function getCommandArgv(key:string):any{
    if(!_isInit){
        _Serialization(process.argv);
        _isInit=true;
    }
    return _keys[key];
}

/**
 * 序列化参数
 */
export function _Serialization(argv:string[]){
    argv.forEach((value,index,array)=>{
        let start = value.indexOf("--");
        let end = value.indexOf(" ");
        if(start!=-1 && end!=-1 ){
            let k = value.substring(start,end).trim();
            let v = value.substring(end).trim();
            _keys[k] = v;
        }else if(start!=-1 ){
            if(array[index+1]==null){
                array[index+1]='';
            }
            _keys[value]=array[index+1].trim();
        }
    });
}
